//
//  XLUserBagViewController.m
//  XiangLove
//
//  Created by 宋振 on 2018/8/3.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLUserBagViewController.h"
#import "TopUpViewController.h"
#import "XLConsumeViewController.h"
#import "XLReChargeViewController.h"
#import "XLBMHCashOutCell.h"
#import "XLCardVucherViewController.h"
#import "BMHCashOutViewController.h"
#import "XLIncomeRecordBaseVC.h"
#import "XLAnchorCommitInforVC.h"

@interface XLUserBagViewController ()<UITableViewDelegate,UITableViewDataSource>

/**
 界面容器
 */
@property (nonatomic, strong) UITableView *  tableView;

//区头
@property (nonatomic, strong) UIView *  headerView;

//金豆
@property (nonatomic, strong) UILabel *  beansLbl;

//提现
@property (nonatomic, strong) UILabel *  moneyLbl;

@end

@implementation XLUserBagViewController
{
    //剩余金豆
    NSString *beans;
    
    //可提现金额
    NSString *money;
    
    //卡券个数
    NSString *cardsCount;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"想爱钱包";
    
    [self showBackButton:NULL];
    
    money = @"0";
    beans = @"0";
    cardsCount = @"0";
    
    /**
     绘制界面
     */
    [self setCustomeView];
    
    /**
     请求数据
     */
    [self requestSource];
    
}



/**
 请求数据
 */
- (void)requestSource
{
    [[[XLHTTPSessionManager sharedManager] XLV2GET:@"/api/v2/Balance/GetUserBalance" parameters:@{@"userId":[[XLAccountManager shareInstance]model].Id} resultClass:nil] subscribeNext:^(id x) {
        
        NSDictionary *dic = x;
        
        [self setupData:dic];
        
    } error:^(NSError *error) {
        
    }];
}

/**
 绘制界面显示
 */
- (void)setCustomeView
{
    self.view.backgroundColor = RGB(246, 246, 246);
    
    CGFloat naviHeight = IS_IPhoneX ? 88.0 : 64.0;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-naviHeight) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.scrollEnabled = NO;
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[XLBMHCashOutCell class] forCellReuseIdentifier:NSStringFromClass([XLBMHCashOutCell class])];
}

/**
 设置区头
 */
- (UIView *)setTableViewHeaderView
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 240)];
    
    header.backgroundColor = [UIColor whiteColor];
    
    
    /**
     提现部分
     */
    //钻石图标
    UIImageView *picTitleView = [[UIImageView alloc] init];
    
    picTitleView.image = [UIImage imageNamed:@"common_qian_icon_jb_n"];
    
    [header addSubview:picTitleView];
    
    //
    [picTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(header.mas_top).offset(21);
        make.left.equalTo(header.mas_left).offset(15);
        make.width.height.mas_equalTo(24);
        
    }];
    
    //提示标题
    UILabel *cashLabel = [[UILabel alloc] init];
    cashLabel.font = [UIFont systemFontOfSize:13];
    cashLabel.textColor = HEXCOLOR(0x9395a2);
    cashLabel.text = @"收入";
    [header addSubview:cashLabel];
    [cashLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(picTitleView.mas_centerY);
        make.left.equalTo(picTitleView.mas_right).offset(3);
        make.height.mas_equalTo(17);
    }];
    
    //剩余金豆显示
    self.moneyLbl = [[UILabel alloc] init];
    self.moneyLbl.font = [UIFont systemFontOfSize:40];
    self.moneyLbl.textColor = RGB(17, 17, 17);
    self.moneyLbl.tintColor = [UIColor darkGrayColor];
    self.moneyLbl.text = [NSString stringWithFormat:@"￥%@",money];
    [header addSubview:self.moneyLbl];
    [self.moneyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(picTitleView.mas_left);
        make.top.equalTo(picTitleView.mas_bottom).offset(1);
        make.right.equalTo(header.mas_right).offset(-88);
        make.height.mas_equalTo(56);
    }];
    
    
    //提现按钮
    UIButton *cashBt = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    NSMutableAttributedString *cashAttribute = [[NSMutableAttributedString alloc] initWithString:@"提现" attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:RGB(17, 17, 17)}];
    
    [cashBt setAttributedTitle:cashAttribute forState:(UIControlStateNormal)];
    
    cashBt.frame = CGRectMake(0, 0, 74, 36);
    cashBt.layer.cornerRadius = 3;
    cashBt.layer.masksToBounds = YES;
    cashBt.layer.borderWidth = 1;
    cashBt.layer.borderColor = [UIColor darkTextColor].CGColor;
    
    [header addSubview:cashBt];
    
    [cashBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(header.mas_top).offset(45);
        make.right.equalTo(header.mas_right).offset(-14);
        make.size.mas_equalTo(CGSizeMake(74, 36));
    }];
    
    [cashBt addTarget:self action:@selector(cashMoney:) forControlEvents:(UIControlEventTouchUpInside)];
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
        cashBt.hidden = YES;
    }
    
    /**
     分割线
     */
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = RGBA(238, 238, 238, 1);
    [header addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header.mas_left).offset(15);
        make.right.equalTo(header.mas_right).offset(-15);
        make.centerY.equalTo(header.mas_centerY);
        make.height.mas_equalTo(0.5);
    }];
    
    
    /**
     充值部分
     */
    
    //钻石图标
    UIImageView *picView = [[UIImageView alloc] init];
    
    picView.image = [UIImage imageNamed:@"common_qian_icon_jb_n"];
    
    [header addSubview:picView];
    
    //
    [picView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(lineView.mas_top).offset(21);
        make.left.equalTo(header.mas_left).offset(15);
        make.width.height.mas_equalTo(24);
        
    }];
    
    //提示标题
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = HEXCOLOR(0x9395a2);
    label.text = @"金豆";
    [header addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(picView.mas_centerY);
        make.left.equalTo(picView.mas_right).offset(3);
        make.height.mas_equalTo(17);
    }];
    
    //剩余金豆显示
    self.beansLbl = [[UILabel alloc] init];
    self.beansLbl.font = [UIFont systemFontOfSize:40];
    self.beansLbl.textColor = RGB(17, 17, 17);
    self.beansLbl.tintColor = [UIColor darkGrayColor];
    self.beansLbl.text = beans;
    [header addSubview:self.beansLbl];
    [self.beansLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(picView.mas_left);
        make.top.equalTo(picView.mas_bottom).offset(1);
        make.right.equalTo(header.mas_right).offset(-88);
        make.height.mas_equalTo(56);
    }];
    
    
    //提现按钮
    UIButton *reChargeBt = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:@"充值" attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:RGB(17, 17, 17)}];
    
    [reChargeBt setAttributedTitle:attribute forState:(UIControlStateNormal)];
    
    reChargeBt.frame = CGRectMake(0, 0, 74, 36);
    reChargeBt.layer.cornerRadius = 3;
    reChargeBt.layer.masksToBounds = YES;
    reChargeBt.layer.borderWidth = 1;
    reChargeBt.layer.borderColor = [UIColor darkTextColor].CGColor;
    
    [header addSubview:reChargeBt];
    
    [reChargeBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_top).offset(45);
        make.right.equalTo(header.mas_right).offset(-14);
        make.size.mas_equalTo(CGSizeMake(74, 36));
    }];
    
    [reChargeBt addTarget:self action:@selector(reChargerMoney:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return header;
}

//提现
- (void)cashMoney:(UIButton *)sender
{
    if([[[XLAccountManager shareInstance] model].IsAnchor isEqualToString:@"1"])
    {
        BMHCashOutViewController *vc = [[BMHCashOutViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }else
    {
        UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"只有通过男神/女神认证后才可以提现" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        if ([[[XLAccountManager shareInstance] model].Status integerValue] != -1) {
            UIAlertAction * sure = [UIAlertAction actionWithTitle:@"马上认证" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                XLAnchorCommitInforVC *vc = [[XLAnchorCommitInforVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }];
            UIAlertAction * canel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alter addAction:sure];
            [alter addAction:canel];
            [self presentViewController:alter animated:YES completion:nil];
        }else
        {
            UIAlertAction * sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alter addAction:sure];
            [self presentViewController:alter animated:YES completion:nil];
        }
        
    }
    
}

//充值
- (void)reChargerMoney:(UIButton *)sender
{
    TopUpViewController *topUpVc = [[TopUpViewController alloc] init];
    
    [self.navigationController pushViewController:topUpVc animated:YES];
    
}

#pragma mark    ---UITableViewDelegate,UITableViewDataSource---
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
        return 2;
    }
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section==0) {
        return 0;
    }else if (section == 1) {
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
            return 2;
        }
        return 1;
    }
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
        return 2;
    }
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XLBMHCashOutCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XLBMHCashOutCell class]) forIndexPath:indexPath];
    
    if (indexPath.section == 1 && ![[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {
        
        cell.title = @"卡券";
        cell.countLbl.hidden = NO;
        cell.countLbl.text = cardsCount;
        
        return cell;
        
    }else {
        
        cell.countLbl.hidden = YES;
        
        if (indexPath.row == 0) {
            cell.title = @"充值记录";
        }else if(indexPath.row == 1) {
            cell.title = @"消费记录";
        }else if (indexPath.row == 2) {
            cell.title = @"收入详情";
        }
        
        return cell;
    }
}

//设置区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        _headerView = [self setTableViewHeaderView];
        
        return _headerView;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section > 0) {
        return 0.1;
    }
    
    return 240;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1 && ![[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {
        
        //卡券
        
        XLCardVucherViewController *cardVucherVc = [[XLCardVucherViewController alloc] init];
        
        [self.navigationController pushViewController:cardVucherVc animated:YES];
        
    }else{
        
        //充值 消费
        
        if (indexPath.row == 0) {
            
            XLReChargeViewController *reChagerVc = [[XLReChargeViewController alloc] init];
            
            [self.navigationController pushViewController:reChagerVc animated:YES];
            
        }else if(indexPath.row == 1) {
            XLConsumeViewController *consumeVc = [[XLConsumeViewController alloc] init];
            
            [self.navigationController pushViewController:consumeVc animated:YES];
        }else if (indexPath.row == 2) {
            XLIncomeRecordBaseVC *vc = [[XLIncomeRecordBaseVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
    

}

- (void)setupData:(NSDictionary *)dic {
    
    beans = F(@"%.lf", [[dic objectForKey:@"AccountBalance"] floatValue]*10);
    
    money = F(@"%@", [dic objectForKey:@"TotalMoney"]);
    
    cardsCount = F(@"%@", [dic objectForKey:@"CardsCount"]);
    
    [self.tableView reloadData];

}



@end
